<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:ebfuncs="http://code.google.com/p/eb-forms/functions"
	xmlns:ebforms="http://code.google.com/p/eb-forms"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xf="http://www.w3.org/2002/xforms"
	xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecification:2"
	xmlns:ev="http://www.w3.org/2001/xml-events"
	version="2.0" exclude-result-prefixes="ebfuncs ebforms">

	<xsl:import href="GlobalVars.xsl" />
	<xsl:import href="StyleCss.xsl" />
	<xsl:import href="Instances.xsl" />
	<xsl:import href="Bindings.xsl" />
	<xsl:import href="Gui.xsl" />

	<xsl:output name="resultFormat" media-type="application/xml" method="xml" 
		encoding="UTF-8" version="1.0" indent="yes" omit-xml-declaration="yes"/>

	<xsl:template match="/ebforms:transform-config/ebforms:schema">
		<xsl:message>Generating Form...</xsl:message>
		<xsl:call-template name="GenerateForm" />
	</xsl:template>
	
	<xsl:template name="GenerateForm">
		<xsl:variable name="output" 
			select="concat( /ebforms:transform-config/ebforms:output/ebforms:dir/@src
			, '/', /ebforms:transform-config/ebforms:output/ebforms:filename/@src )" />
		
		<xsl:result-document href="{$output}" format="resultFormat">
			<html>
				<xsl:copy-of select="$global.doc.schema.root/xsd:schema/namespace::ccts" />
				<xsl:copy-of select="$global.doc.schema.root/xsd:schema/namespace::udt" />
				<xsl:copy-of select="$global.doc.schema.root/xsd:schema/namespace::qdt" />
				<xsl:copy-of select="$global.doc.schema.root/xsd:schema/namespace::ram" />
				<xsl:copy-of select="$global.doc.schema.root/xsd:schema/namespace::rsm" />
				
				<!--
				<types><xsl:sequence select="$global.element.types.used" /></types>
				-->
								
				<head>
					<title>
						<xsl:value-of 
							select="$global.doc.schema.root/xsd:schema/xsd:element[1]/xsd:annotation/xsd:documentation/ccts:Name" />
					</title>
					
					<xsl:if test="$global.browser = 'IE' ">
						<object id="FormsPlayer" width="0" height="0" 
							classid="CLSID:4D0ABA11-C5F0-4478-991A-375C4B648F58">
							<b>FormsPlayer has failed to load! Please check your installation.</b>
						</object>
						<xsl:processing-instruction name="import">namespace="xf" implementation="#FormsPlayer"</xsl:processing-instruction>
					</xsl:if>
					
					<xsl:message>Generating css ...</xsl:message>
					<xsl:apply-templates mode="style" 
						select="$global.doc.schema.root/xsd:schema/xsd:element[1]" />
					
					<xf:model id="M-1" schema="{document-uri( $global.doc.schema.root )}">
						<xsl:message>Generating instance code ...</xsl:message>
						<xsl:apply-templates mode="instances" 
							select="$global.doc.schema.root/xsd:schema/xsd:element[1]"  />
						
						<xsl:message>Generating binding code ...</xsl:message>
						<xsl:apply-templates mode="bindings"
							select="$global.doc.schema.root/xsd:schema/xsd:element[1]" />
					
						<!-- 
							When the transformMode is new or draft the XForm should supply the 
							posibillity to save the draft.
						-->
						<xsl:if test="ebfuncs:transformMode() = 'new' or ebfuncs:transformMode() = 'draft' ">
							<xf:submission id="message.draft.save" method="put" 
								action="{ebfuncs:instanceFile()}" replace="none" 
								includenamespaceprefixes="#default" instance="instance.message" />
							
							<xf:submission id="message.send" method="put" 
								action="http://localhost:8088/place_order" replace="none" 
								includenamespaceprefixes="#default" instance="instance.message" />
						</xsl:if>
						
						<!-- 
							When the transformMode is draft the XForm should supply the 
							posibillity to undo the changes after the last save.
						-->
						<xsl:if test="ebfuncs:transformMode() = 'draft'">
							<xf:submission id="message.draft.load" action="{ebfuncs:instanceFile()}"
								replace="instance" method="get" />
						</xsl:if>
						
						<xsl:if test="ebfuncs:transformMode() = 'read'">
							<xsl:variable name="message.location" 
								select="if( ebfuncs:browser() = 'other' ) 
									then concat( 'file:../inbox/', substring-after( ebfuncs:instanceFile(), '=' ) )
									else concat( '../inbox/', substring-after( ebfuncs:instanceFile(), '=' ) )" />
							
							<xf:submission id="message.save" method="put" 
								action="{$message.location}" replace="none"
								includenamespaceprefixes="#default"
								instance="instance.message"  />
							
							
							<!-- This is lame! Change this after the demo -->
							<xf:action ev:event="xforms-ready">
								<xf:send submission="message.save" />
							</xf:action>
						</xsl:if>
					</xf:model>
				</head>
				<body>
					<xsl:message>Generating gui ...</xsl:message>
					<xsl:apply-templates mode="gui"
						select="$global.doc.schema.root/xsd:schema/xsd:element[1]" />
					
					<xsl:if test="ebfuncs:transformMode() = 'new' or ebfuncs:transformMode() = 'draft' ">
						<!--
						<xf:submit submission="message.draft.save">
							<xf:label>Sla op als draft</xf:label>
						</xf:submit>
						-->
						<!--
						<xf:submit submission="message.send">
							<xf:label>Verstuur bericht</xf:label>
						</xf:submit>
						-->
						<xf:trigger>
							<xf:label>Verstuur bericht (js)</xf:label>
							<xf:action ev:event="DOMActivate">
								<xf:load resource="javascript:parent.sendMessage()"/>
							</xf:action>
						</xf:trigger>
					</xsl:if>
				</body>
			</html>
		</xsl:result-document>
	</xsl:template>
	
	<xsl:template match="xsd:element" mode="style">
		<xsl:call-template name="GenerateStyle" >
			<xsl:with-param name="element.type" select="@type" />
		</xsl:call-template>
	</xsl:template>
	
	<!-- 
		Creates the instances for the XForms document. This is in the first place the instance
		for the message to send. And in the second place the instances that refer to the codelists
		that are used in the message schema.
		
		@template build-instances - see Modules/instances.xsl
	-->
	<xsl:template match="xsd:element" mode="instances">
		<xsl:call-template name="GenerateInstances">
			<xsl:with-param name="element.root" select="@name" />
			<xsl:with-param name="element.root.prefix" select="$global.schema.root.prefix" />
		</xsl:call-template>
	</xsl:template>

	<!-- 
		Creates the bindings in the XForms model. It binds all nodes in the message
		too the datatype as defined in the schema.
		
		@template build-bindings - see Modules/bindings.xsl
	-->
	<xsl:template match="xsd:element" mode="bindings">
		<xsl:call-template name="GenerateBindings">
			<xsl:with-param name="element.root" select="@name" />
			<xsl:with-param name="element.root.type" select="@type" />
		</xsl:call-template>
	</xsl:template>
	
	<!-- 
		Creates the gui elements of the XForms document.
		
		@template build-gui - see Modules/gui.xsl
	-->
	<xsl:template match="xsd:element" mode="gui">
		<xsl:call-template name="GenerateGui">
			<xsl:with-param name="type" select="@type" />
			<xsl:with-param name="prefix">
				<xsl:value-of select="$global.schema.root.prefix" />
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>
</xsl:stylesheet>